﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Reflection;

namespace AlysaLayer
{
    public partial class FormAlysa : Form
    {
        List<Alysa.Plugin> mPlugins = new List<Alysa.Plugin>();
        int x = 3;

        public FormAlysa()
        {
            InitializeComponent();
        }

        private void ButtonCppAdd_Click(object sender, EventArgs e)
        {


        }

        private void loadPlugin (String dll, String className, String name)
        {
            try
            {
                Assembly assembly = Assembly.LoadFrom(dll);

                Type[] types = assembly.GetTypes();

                Type type = assembly.GetType(className);

                object pluginInstance = Activator.CreateInstance(type);

                Alysa.Plugin wPluging = (Alysa.Plugin)pluginInstance;

                wPluging.Initialize(name);

                mPlugins.Add(wPluging);
            }
            catch (Exception)
            {
                MessageBox.Show("Can't load plugin " + name + " from " + dll, "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }


        private void FormAlysa_Load(object sender, EventArgs e)
        {
            System.IO.StreamReader file = new System.IO.StreamReader("Composition.ai.txt");
            String line; 
            while ((line = file.ReadLine()) != null)
            {
                if(line.Length > 2 
                    && line.StartsWith("//"))
                {
                    continue;
                }
                else
                {
                    String[] parts = line.Split(';');
                    String wPluginName = parts[0];
                    String wClassName = parts[1];
                    String wDllAddress = parts[2];
                    loadPlugin(wDllAddress, wClassName, wPluginName);
                }
            }

            file.Close();

            //loadPlugin(@"C:\Users\Yuri\Documents\Visual Studio 2010\Projects\Alysa Repo\AlysaLayer\CppAddPlugin\bin\Debug\CppAddPlugin.dll", "CppAddPluginWrapper.CppAddWrapperPlugin", "C++ Single Thread Adder");
            //loadPlugin(@"C:\Users\Yuri\Documents\Visual Studio 2010\Projects\Alysa Repo\AlysaLayer\CppAddPlugin\bin\Debug\CppAddPlugin(1).dll", "CppAddPluginWrapper.CppAddWrapperPlugin", "Pizza and Sushi Plugin");
            //loadPlugin(@"C:\Users\Yuri\Documents\Visual Studio 2010\Projects\Alysa Repo\AlysaLayer\CppAddPlugin\bin\Debug\CppAddPlugin(2).dll", "CppAddPluginWrapper.CppAddWrapperPlugin", "Hot Chocolate Addon");
            //loadPlugin(@"C:\Users\Yuri\Documents\Visual Studio 2010\Projects\Alysa Repo\AlysaLayer\CppAddPlugin\bin\Debug\CppAddPlugin(3).dll", "CppAddPluginWrapper.CppAddWrapperPlugin", "Blue Screen Extension");
           // loadPlugin(@"C:\Users\Yuri\Documents\Visual Studio 2010\Projects\Alysa Repo\AlysaLayer\CppAddPlugin\bin\Debug\CppAddPlugin(4).dll", "CppAddPluginWrapper.CppAddWrapperPlugin", "Alissa Pomme Peo Plugin");

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            bool wStillRunning = false;

            Parallel.ForEach<Alysa.Plugin>
             (mPlugins,
             wPlugin =>
             {
                 if (wPlugin.getResult().getStatus() == Alysa.PluginExecutionResult.STATUS.IN_PROGRESS)
                 {
                     wStillRunning = true;
                 }
             }
             );

            if (wStillRunning)
            {
                MessageBox.Show("Still running. Try cancel or stop.", "Can't start", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
          
            Parallel.ForEach<Alysa.Plugin>
             (mPlugins,
             wPlugin =>
                { 
                    wPlugin.start(new Alysa.PluginInputData(x, x+1)); 
                }
             );
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            for (int i = 0; i < mPlugins.Count; ++i)
            {
                if (mPlugins[i].getResult().getStatus() == Alysa.PluginExecutionResult.STATUS.SUCCESS)
                {
                    listBox1.Items.Add(mPlugins[i].getName() + ", " + mPlugins[i].getResult().getStatus().ToString() + " (result = " + mPlugins[i].getResult().getResult() + ")");
                }
                else
                {
                    listBox1.Items.Add(mPlugins[i].getName() + ", " + mPlugins[i].getResult().getStatus().ToString());
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Parallel.ForEach<Alysa.Plugin>
             (mPlugins,
             wPlugin =>
             {
                 if (wPlugin.getResult().getStatus() == Alysa.PluginExecutionResult.STATUS.IN_PROGRESS)
                 {
                     wPlugin.RequestStop();
                 }
             }
             );
             
            //for (int i = 0; i < mPlugins.Count; ++i) { mPlugins[i].RequestStop(); }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Parallel.ForEach<Alysa.Plugin>
             (mPlugins,
             wPlugin =>
             {
                 if (wPlugin.getResult().getStatus() == Alysa.PluginExecutionResult.STATUS.IN_PROGRESS)
                 {
                     wPlugin.stop();
                 }
             }
             );
        }

        private void FormAlysa_FormClosing(object sender, FormClosingEventArgs e)
        {
            button3_Click(sender, e);
        }
    }
}
